package com.lujianfei.powersearch.database

import android.os.Parcel
import android.os.Parcelable
import androidx.databinding.ObservableField
import com.orm.SugarRecord
import com.orm.dsl.Column
import com.orm.dsl.Ignore
import com.orm.dsl.Table
import java.text.SimpleDateFormat
import java.util.*

@Table(name = "SearchItemModel")
data class SearchItemModel(
    @Column(name = "name")
    var name: String? = null,
    @Column(name = "parentId")
    var parentId: Long = -1,
    @Column(name = "updateTime")
    var updateTime: Long = 0,
    @Column(name = "createTime")
    var createTime: Long = 0,
    @Ignore
    var level: Int = 0, //用于层级排序
    @Column(name = "imagePath")
    var imagePath: String? = "",
    @Column(name = "keywords")
    var keywords: String? = "",
    @Column(name = "location")
    var location: String? = "",
    @Column(name = "cost")
    var cost: String? = "",
    @Column(name = "is_top")
    var is_top: Boolean = false,
    @Column(name = "sort")
    var sort: Long = 0,
    @Column(name = "package_name")
    var packageName: String? = "",
    @Column(name = "app_name")
    var appName: String? = "",
    @Column(name = "start_time")
    var startTime: Long = 0,
    @Column(name = "end_time")
    var endTime: Long = 0,
    @Ignore
    var countDownStart:ObservableField<String>?= null,
    @Ignore
    var countDownEnd:ObservableField<String>?= null,
    @Ignore
    var selectId:Long = -1,
    @Ignore
    var subCount:Int = -1,
) : SugarRecord(), Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readLong(),
        parcel.readLong(),
        parcel.readLong(),
        parcel.readInt(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readString(),
        parcel.readBoolean(),
        parcel.readLong(),
        parcel.readString(),
        parcel.readString(),
        parcel.readLong(),
        parcel.readLong(),
        ObservableField(parcel.readString()),
        ObservableField(parcel.readString()),
        parcel.readLong(),
    ) {
        id = parcel.readLong()
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeLong(parentId)
        parcel.writeLong(updateTime)
        parcel.writeLong(createTime)
        parcel.writeInt(level)
        parcel.writeString(imagePath)
        parcel.writeString(keywords)
        parcel.writeString(location)
        parcel.writeString(cost)
        parcel.writeBoolean(is_top)
        parcel.writeLong(sort)
        parcel.writeString(packageName)
        parcel.writeString(appName)
        parcel.writeLong(startTime)
        parcel.writeLong(endTime)
        parcel.writeString(countDownStart?.get())
        parcel.writeString(countDownEnd?.get())
        parcel.writeLong(selectId)

        parcel.writeLong(id)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<SearchItemModel> {
        override fun createFromParcel(parcel: Parcel): SearchItemModel {
            return SearchItemModel(parcel)
        }

        override fun newArray(size: Int): Array<SearchItemModel?> {
            return arrayOfNulls(size)
        }
    }

    fun costDisplay(): String {
        kotlin.runCatching {
            return String.format("%.2f", cost?.toDouble())
        }
        return ""
    }

    fun getStartTimeStr():String {
        if (startTime == 0L) return ""
        return getTimeStr(timeStamp = startTime)
    }

    fun getEndTimeStr():String {
        if (endTime == 0L) return ""
        return getTimeStr(timeStamp = endTime)
    }

    fun getCreateTimeStr():String {
        if (createTime == 0L) return ""
        return getTimeStr(timeStamp = createTime)
    }

    fun getUpdateTimeStr():String {
        if (updateTime == 0L) return ""
        return getTimeStr(timeStamp = updateTime)
    }

    fun getDurationStr():String {
        if (endTime == 0L || startTime == 0L) return ""
        var fStarttime = startTime
        var fEndtime = endTime
        if ("$fStarttime".length >= 13) {
            fStarttime /= 1000
        }
        if ("$fEndtime".length >= 13) {
            fEndtime /= 1000
        }
        return getDurationTimeStr(duration = fEndtime - fStarttime)
    }

    private fun getDurationTimeStr(duration:Long) :String {
        var seconds = duration
        var minutes = 0
        var hours = 0
        var days = 0
        if (seconds >=60) {
            minutes = (seconds / 60).toInt()
            seconds %= 60
        }
        if (minutes >= 60) {
            hours = minutes / 60
            minutes %= 60
        }
        if (hours >= 24) {
            days = hours / 24
            hours %= 24
        }
        val strBuilder = StringBuilder()
        if (days > 0) {
            strBuilder.append("${days}天")
        }
        if (hours > 0) {
            strBuilder.append("${hours}小时")
        }
        if (minutes > 0) {
            strBuilder.append("${minutes}分钟")
        }
        if (seconds > 0) {
            strBuilder.append("${seconds}秒")
        }
        return strBuilder.toString()
    }
    private fun getTimeStr(timeStamp:Long): String {
        val calendar = Calendar.getInstance()
        val thisYear = calendar.get(Calendar.YEAR)
        val thisMonth = calendar.get(Calendar.MONTH)
        val thisDay = calendar.get(Calendar.DAY_OF_MONTH)

        if ("$timeStamp".length >= 13) {
            calendar.timeInMillis = timeStamp
        } else {
            calendar.timeInMillis = timeStamp * 1000
        }
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)
        val hourOfDay = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)

        if (thisYear == year) {
            if (thisMonth == month) {
                return if (thisDay == day) {
                    val prefixStr = "今天"
                    hourMinute(hourOfDay, minute, prefixStr, calendar.timeInMillis)
                } else if (thisDay + 1 == day) {
                    val prefixStr = "明天"
                    hourMinute(hourOfDay, minute, prefixStr, calendar.timeInMillis)
                } else if (thisDay + 2 == day) {
                    val prefixStr = "后天"
                    hourMinute(hourOfDay, minute, prefixStr, calendar.timeInMillis)
                } else {
                    val prefixStr = SimpleDateFormat("dd号", Locale.CHINA).format(Date(calendar.timeInMillis))
                    hourMinute(hourOfDay, minute, prefixStr, calendar.timeInMillis)
                }
            } else {
                return if (thisMonth + 1 == month) {
                    val prefixStr = SimpleDateFormat("下个月dd号 ", Locale.CHINA).format(Date(calendar.timeInMillis))
                    hourMinute(hourOfDay, minute, prefixStr, calendar.timeInMillis)
                } else {
                    val prefixStr = SimpleDateFormat("MM-dd  ", Locale.CHINA).format(Date(calendar.timeInMillis))
                    hourMinute(hourOfDay, minute, prefixStr, calendar.timeInMillis)
                }
            }
        } else {
            val prefixStr = SimpleDateFormat("yyyy-MM-dd  ", Locale.CHINA).format(Date(calendar.timeInMillis))
            return hourMinute(hourOfDay, minute, prefixStr, calendar.timeInMillis)
        }
    }

    private fun hourMinute(
        hourOfDay: Int,
        minute: Int,
        prefixStr: String,
        timeStamp: Long
    ):String {
        val timeStampF = if (timeStamp >= 13) {
            timeStamp
        } else {
            timeStamp * 1000
        }
        return if (hourOfDay == 0) {
            if (minute == 0) {
                prefixStr
            } else {
                SimpleDateFormat("${prefixStr}HH:mm", Locale.CHINA).format(Date(timeStampF))
            }
        } else {
            if (minute == 0) {
                SimpleDateFormat("${prefixStr}HH点", Locale.CHINA).format(Date(timeStampF))
            } else {
                SimpleDateFormat("${prefixStr}HH:mm", Locale.CHINA).format(Date(timeStampF))
            }
        }
    }
}